import notification from "@/services/notification";
import { observable, action,makeObservable } from 'mobx';
import ContractChange from "@/services/contractchange";
import Contracts from "@/services/contracts";
import ContractChangeModel from "@/store/models/ContractChangeModel";
import GlobalFunctions from '@/store/globalFunctions';
import Workflow from "@/services/workflow";
import CheckDialog from "@/components/workflow/CheckDialog";
import { currentUser } from "@/services/auth";
import ContractApply from "@/services/contractapply";
import moment from "moment";

class ContractChangeStore {
  @observable contractChangeModel = new ContractChangeModel(0);
  constructor() {
    makeObservable(this)
  }
  @action initStore(id) {
    this.init(id);
  }

  init(id) {
    if (id !== "0" && id !== 0) {
        ContractChange.get({ id: id }).then(data => {
        this.contractChangeModel = ContractChangeModel.fromJS(data);
        Workflow.edit({ bill: "contractchange", billtype: 0, billid: id, userid: currentUser.id }).then(data1 => {
          if (data1.length > 0) {
            this.contractChangeModel.setBilledit(data1[0]);
          }
        }).catch(error => {
        });
        
      }).catch(error => {
        this.contractChangeModel = new ContractChangeModel(id);
        this.getcode("contractchange",moment(),0,"code");
      });
    } else {
        ContractChange.getid({ id: 0 }).then(data => {
        this.contractChangeModel = new ContractChangeModel(data[0].id);
        this.getcode("contractchange",moment(),0,"code");
      });
    }
  }
  getcode(tblname,bill_date,type,tblfield) {
    ContractApply.getcode( {tblname: tblname,bill_date: bill_date,type: type,tblfield: tblfield,}).then(data => {
      this.contractChangeModel.setCode(data[0].code);
    }).catch(error => {
      notification.error(error.message, error.detailedMessage);
    });
  };

  dataSave() {
    if(!currentUser.isDefault){
      notification.error("没有权限.");
      return false;
    }
    if(this.contractChangeModel.templatecontent.length<10){
      notification.error("请先提交合同正文！");
      return false;
    }
    if(this.contractChangeModel.status ===0 && currentUser.id !==this.contractChangeModel.userid){
      notification.error("草稿状态，不是制单人，不能保存！");
      return false;
    }
    if(this.contractChangeModel.status ===2){
        notification.error("已审批，不能保存！");
        return false;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.contracttype)){
      notification.error("合同类别不能为空");
      return false;
    }else{
      if(this.contractChangeModel.contracttype.length<1){
        notification.error("合同类别不能为空");
        return false;
      }
    }
    if(this.contractChangeModel.contracttype.indexOf("框架协议") >=0){
      this.contractChangeModel.budgetcode ="";
      this.contractChangeModel.pay_direction=10;
      this.contractChangeModel.zhengcai=0;
      this.contractChangeModel.puremployeeid=0;
      this.contractChangeModel.pricetype=10;
      this.contractChangeModel.amount=0;
      this.contractChangeModel.amount_words="";
      this.contractChangeModel.amounttype =[];
    }else{
      if(GlobalFunctions.isEmptyobj(this.contractChangeModel.pay_direction)){
        notification.error("收付方向不能为空");
        return false;
      }
      if(GlobalFunctions.isEmptyobj(this.contractChangeModel.pricetype)){
        notification.error("合同类型不能为空");
        return false;
      }
      if(this.contractChangeModel.amounttype.length===0){
        notification.error("资金类别不能为空！");
        return false;
      }
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.employeeid)){
      notification.error("经办人不能为空！");
      return false;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.departmentid)){
      notification.error("经办部门不能为空");
      return false;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.code)){
      notification.error("合同编号不能为空");
      return false;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.name)){
      notification.error("合同名称不能为空");
      return false;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.type)){
      this.contractChangeModel.type=[]
      //notification.error("合同分类不能为空");
      //return;
    }
    if(this.contractChangeModel.empowersex==="是"){
      if(GlobalFunctions.isEmptyobj(this.contractChangeModel.empowerperson)){
        notification.error("授权代理人不能为空");
        return false;
      }
      if(GlobalFunctions.isEmptyobj(this.contractChangeModel.empowerduties)){
        notification.error("代理人职务不能为空");
        return false;
      }
    }
    if(!GlobalFunctions.isEmptyobj(this.contractChangeModel.message)){
      if(this.contractChangeModel.message.length>1050){
        notification.error("重要条款不能超过1000字,现在有"+this.contractChangeModel.message.length+"字");
        return false;
      }
    }
    if(this.contractChangeModel.pricetype===0){
      if(this.contractChangeModel.amount<1){
        notification.error("总价合同金额不能为空");
        return false;
      }
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.party_aid)){
      notification.error("甲方不能为空");
      return false;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.message)){
      notification.error("重要条款不能为空");
      return false;
    }
    if(this.contractChangeModel.isbulu<2){
      if(GlobalFunctions.isEmptyobj(this.contractChangeModel.warn_date)){
        notification.error("提醒日期不能为空");
        return false;
      }
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.party_bid) || this.contractChangeModel.party_bid==0){
      this.contractChangeModel.party_bid=0;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.party_cid)){
      this.contractChangeModel.party_cid=0;
    }
    if(GlobalFunctions.isEmptyobj(this.contractChangeModel.puremployeeid)){
      this.contractChangeModel.puremployeeid=0;
    }


    let data={id: this.contractChangeModel.id,//
            userid: this.contractChangeModel.userid,//--操作员ID
            bill_date: this.contractChangeModel.bill_date,//--日期
            employeeid: this.contractChangeModel.employeeid,//--经办人
            departmentid: this.contractChangeModel.departmentid,//--经办部门
            code: this.contractChangeModel.code,//----合同编号
            name: this.contractChangeModel.name,//--合同名称
            officialcode: this.contractChangeModel.officialcode,
            budgetcode: this.contractChangeModel.budgetcode,
            recordcode: this.contractChangeModel.recordcode,
            puremployeeid:this.contractChangeModel.puremployeeid,
            templateid: this.contractChangeModel.templateid,
            templatecontent: this.contractChangeModel.templatecontent,
            type: this.contractChangeModel.type,//--合同类别
            party_aid: this.contractChangeModel.party_aid,//--甲方ID
            party_bid: this.contractChangeModel.party_bid ,//--乙方
            party_cid: this.contractChangeModel.party_cid ,//--其它关联方
            pay_direction: this.contractChangeModel.pay_direction ,//,--收付方向0-fukuan,1-shoukuan
            infrastructure: this.contractChangeModel.infrastructure,
            zhengcai:this.contractChangeModel.zhengcai,
            iskeyan:this.contractChangeModel.iskeyan,
            iszhaobiao:this.contractChangeModel.iszhaobiao,
            warnsealuser:this.contractChangeModel.warnsealuser,
            isbulu:this.contractChangeModel.isbulu,
            pricetype: this.contractChangeModel.pricetype,
           sign_date: this.contractChangeModel.sign_date,//--合同签订日期
           start_date: this.contractChangeModel.start_date,//--合同开始日期
           end_date: this.contractChangeModel.end_date,//--合同终止日期
           warn_date: this.contractChangeModel.warn_date,
           warn_checkdate:this.contractChangeModel.warn_checkdate,
           warn_message:this.contractChangeModel.warn_message,//提醒内容
           warn_check:this.contractChangeModel.warn_check,//审核提醒
           volumepurchase:this.contractChangeModel.volumepurchase,
            amount: this.contractChangeModel.amount,//--金额
            amount_words:this.contractChangeModel.amount_words,//----合同金额(大写)
            amounttype: this.contractChangeModel.amounttype,
            contracttype: this.contractChangeModel.contracttype,
            status: this.contractChangeModel.status,//--状态
            sourcetype: this.contractChangeModel.sourcetype,
            sourceid: this.contractChangeModel.sourceid,
            empowerperson: this.contractChangeModel.empowerperson,//授权人
            empowersex: this.contractChangeModel.empowersex,//性别
            empowerduties: this.contractChangeModel.empowerduties,//职务
            agreementcount: this.contractChangeModel.agreementcount,//协议份数
            contractsid: this.contractChangeModel.contractsid,
            contractcode: this.contractChangeModel.contractcode,
            changebefore: this.contractChangeModel.changebefore,
            message: this.contractChangeModel.message}
    if(this.contractChangeModel.isnew) {
        ContractChange.create(data)
        .then(() => {
          this.init(this.contractChangeModel.id);
          notification.success("新增成功.");
          return true;
        })
        .catch(error => {
          notification.error(error.message, error.response.data.message);
          return false;
        });
    }else{
        ContractChange.save(data)
        .then(() => {
          notification.success("修改成功.");
          return true;
        })
        .catch(error => {
          notification.error(error.message, error.response.data.message);
          return false;
        });
    }
  };
  dataDelete() {
    ContractChange.delete({ id: this.contractChangeModel.id }).then(() => {
      notification.success("删除成功.");
      this.init(0);
    }).catch(error => {
      notification.error("删除失败", error.response.data.message);
    });
  };
  getContract(contractid) {
    Contracts.get({ id: contractid }).then(data => {
        this.contractChangeModel.setContractsid(data);        
      }).catch(error => {
      });
    }
  billCheck(userid) {
    if(!currentUser.isAudit){
      if (this.contractChangeModel.status===0 || (this.contractChangeModel.status === 1 && this.contractChangeModel.userid === currentUser.id)){

      }else{
        notification.error("没有权限.");
        return;
      }
    }
    if(this.contractChangeModel.isnew){
      this.dataSave();
      return ;
    }
    let item = {
      bill: "contractchange",
      billtype: 0,
      billid: this.contractChangeModel.id,
      next_type: 1,
      message:  "<div>同意</div>",
      userid: userid,
      billdate: this.contractChangeModel.bill_date.format('YYYY-MM-DD'),
      orgunitid: this.contractChangeModel.departmentid,
      status: this.contractChangeModel.status,
      warnsealuser: this.contractChangeModel.warnsealuser,
      billuserid: userid,
    }
      CheckDialog.showModal({ bill_check: item })
        .onClose((result = {}) => {
          if (result.success) {
            this.init(this.contractChangeModel.id);
          }
        }).onDismiss(() => {
        });
    // }
  };
}

export default ContractChangeStore;